#include "../../run/CR.hpp"

int main()
{
    DFILESYNC();
    // 完成接收客户端Json::Value，处理、运行，然后把标准错误标准输出输出到string
    /* ******************************
     * 客户端发送Json格式：
     * code:
     * stdin:
     * cpu_limit:
     * mem_limit:
     * 服务端输出Json格式：
     * desc:
     * error:
     * stdout:
     * stderr:
     ******************************* */
    // 模拟客户端发送来的Json
    Json::Value in_val;
    std::string code = R"(#include <iostream>
    int main()
    {
        std::cout << "你好，这是客户端1！" << std::endl;
        std::cout << "你好，这是客户端1！" << std::endl;
        std::cout << "你好，这是客户端1！" << std::endl；
    })";

    in_val["code"] = code;
    in_val["stdin"] = "this is stdin";
    in_val["cpu_limit"] = 1;
    in_val["mem_limit"] = 0;

    std::string in;
    util_ns::JSON::serializer(in, in_val);

    std::string out;
    CR::handle(in, out);
    Json::Value out_val;
    util_ns::JSON::unserializer(out, out_val);

    std::cout << out_val["desc"].asString() << '\n';
    std::cout << out_val["status"].asInt() << '\n';
    std::cout << out_val["stdout"].asString() << '\n';
    std::cout << out_val["stderr"].asString() << '\n';




    in_val;
    code = R"(#include <iostream>
    int main()
    {
        std::cout << "你好，这是客户端2！" << std::endl;
        std::cout << "你好，这是客户端2！" << std::endl;
        std::cout << "你好，这是客户端2！" << std::endl;
        while(1){};
    })";

    in_val["code"] = code;
    in_val["stdin"] = "this is stdin";
    in_val["cpu_limit"] = 1;
    in_val["mem_limit"] = 1;

    in;
    util_ns::JSON::serializer(in, in_val);

    out;
    CR::handle(in, out);
    out_val;
    util_ns::JSON::unserializer(out, out_val);

    std::cout << out_val["desc"].asString() << '\n';
    std::cout << out_val["status"].asInt() << '\n';
    std::cout << out_val["stdout"].asString() << '\n';
    std::cout << out_val["stderr"].asString() << '\n';

    in_val;
    code = R"(#include <iostream>
    int main()
    {
        std::cout << "你好，这是客户端3！" << std::endl;
        std::cout << "你好，这是客户端3！" << std::endl;
        std::cout << "你好，这是客户端3！" << std::endl;
        int *arr = new int[1000000];
    })";

    in_val["code"] = code;
    in_val["stdin"] = "this is stdin";
    in_val["cpu_limit"] = 1;
    in_val["mem_limit"] = 2;

    in;
    util_ns::JSON::serializer(in, in_val);

    out;
    CR::handle(in, out);
    out_val;
    util_ns::JSON::unserializer(out, out_val);

    std::cout << out_val["desc"].asString() << '\n';
    std::cout << out_val["status"].asInt() << '\n';
    std::cout << out_val["stdout"].asString() << '\n';
    std::cout << out_val["stderr"].asString() << '\n';
}
